home *** CD-ROM | disk | FTP | other *** search
/ CD World 1998 January / CD World - Ocak 1998.iso / misc / dbase55 / disk7 / samples1.pak / EVENT.PRG < prev    next >
Text File  |  1995-07-18  |  14KB  |  355 lines

  1. *******************************************************************************
  2. *  PROGRAM:      Event.prg
  3. *
  4. *  WRITTEN BY:   Borland Samples Group
  5. *
  6. *  DATE:         6/93
  7. *
  8. *  UPDATED:      5/95
  9. *
  10. *  VERSION:      Visual dBASE
  11. *
  12. *  DESCRIPTION:  This program uses the Visual dBASE object model to create
  13. *                forms with event handlers that manipulate the properties of the
  14. *                form and its controls.
  15. *                The ButtonForm class creates a form which manipulates its
  16. *                pushbuttons.
  17. *                When you left click anywhere in that form, the pushbutton
  18. *                will move to that point, and its caption will change to show
  19. *                the current coordinates. You can also Ctrl-Click to
  20. *                make another button, and Shift-Click to align all the buttons.
  21. *
  22. *                The SizeForm class creates another type of form.  When
  23. *                you click and move in this form, its size will change
  24. *                as you move the mouse.
  25. *
  26. *                Indicator messages are attached to events that don't
  27. *                have any actions attached to them.  These messages will
  28. *                display in the Command Results window when the corresponding
  29. *                events occur.  Messages display both for button events and
  30. *                form events.
  31. *
  32. *                All properties are changed using event handling
  33. *                functions that are assigned to these forms.
  34. *
  35. *  PARAMETERS:   None
  36. *
  37. *  CALLS:        Buttons.cc    (Custom Controls file)
  38. *                SetCapture()  (Windows API function)
  39. *
  40. *  USAGE:        DO Event
  41. *
  42. *******************************************************************************
  43. #include <Utils.h>
  44.  
  45. create session
  46. set talk off
  47. set ldCheck off
  48.  
  49. set procedure to program(1) additive
  50. set procedure to &_dbwinhome.samples\Buttons.cc additive
  51.  
  52. extern CWORD SetCapture(CWORD) USER && for sizeForm
  53.  
  54. local bForm, sForm
  55.  
  56. bForm = new ButtonForm("Button Form","Click anywhere in the form to move button")
  57. sForm = new SizeForm("Size Form","Click in the form and move mouse to resize form")
  58. sForm.Open()
  59. bForm.Open()
  60.  
  61.  
  62. *******************************************************************************
  63. *******************************************************************************
  64. class EventForm(name, titleText) of Form
  65.  
  66. * Basic form with a pushbutton in the center
  67. *******************************************************************************
  68.  
  69.    this.top = 0.00
  70.    this.left = 1.35
  71.    this.height = 11.22
  72.    this.width = 51.43
  73.    this.name = name
  74.    this.text = name + ": " + titleText
  75.    this.titleText = titleText
  76.  
  77.    * custom properties
  78.    this.buttonCnt = 1                 && Button count on this form
  79.  
  80.    this.buttonHeight = 3.06           && Initial height and width for buttons
  81.    this.buttonWidth = 10.81
  82.    this.curHeight = this.height       && Initial height and width for form
  83.    this.curWidth  = this.width
  84.    this.totalRows = floor(this.height/this.buttonHeight)  && Total allowed
  85.    this.totalCols = floor(this.width/this.buttonWidth)    && rows, columns
  86.  
  87.    * a button
  88.    this[1] = new EventButton(this)
  89.  
  90.    * Events
  91.    * These are just some of the events you can detect in a form
  92.  
  93.    this.OnOpen          = {;ShowEvent(this.name,"OnOpen")}
  94.    this.OnGotFocus      = {;ShowEvent(this.name,"OnGotFocus")}
  95.    this.OnSize          = CLASS::OnSize
  96.    this.OnLeftMouseDown = {;ShowEvent(this.name,"OnLeftMouseDown")}
  97.    this.OnMove          = {;ShowEvent(this.name,"OnMove")}
  98.    this.OnLostFocus     = {;ShowEvent(this.name,"OnLostFocus")}
  99.    this.OnClose         = {;ShowEvent(this.name,"OnClose")}
  100.  
  101.    * Add sampleInfoButton -- defined in Buttons.cc
  102.  
  103.    define sampleInfoButton eventInfoButton of this;
  104.       property;
  105.          top 0,;
  106.          left this.width - 3.5;
  107.       custom;
  108.          sampleName "Event.prg"
  109.  
  110.  
  111.    ****************************************************************************
  112.    procedure OnSize
  113.    ****************************************************************************
  114.  
  115.    form.eventInfoButton.left = form.width - 3.5
  116.    ShowEvent(form.name,"OnSize")
  117.  
  118.  
  119.  
  120.  
  121.  
  122. endclass   && EventForm
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129. *******************************************************************************
  130. *******************************************************************************
  131. class EventButton(f) of PushButton(f)
  132. *******************************************************************************
  133.  
  134.    this.height = form.buttonHeight
  135.    this.width = form.buttonWidth
  136.    this.top = (f.height - this.height)/2
  137.    this.left = (f.width - this.width)/2
  138.    this.text = ""
  139.    this.fontname = "MS Sans Serif"
  140.    this.fontsize = 15
  141.    this.fontbold = .T.
  142.  
  143.    * Events
  144.    * These are all the events you can detect in a pushbutton
  145.  
  146.    this.OnMouseMove       = {;ShowEvent(form.className+"."+this.name,"OnMouseMove")}
  147.    this.OnRightMouseUp    = {;ShowEvent(form.className+"."+this.name,"OnRightMouseUp")}
  148.    this.OnRightMouseDown  = {;ShowEvent(form.className+"."+this.name,"OnRightMouseDown")}
  149.    this.OnRightDblClick   = {;ShowEvent(form.className+"."+this.name,"OnRightDblClick")}
  150.    this.OnMiddleMouseUp   = {;ShowEvent(form.className+"."+this.name,"OnMiddleMouseUp")}
  151.    this.OnMiddleMouseDown = {;ShowEvent(form.className+"."+this.name,"OnMiddleMouseDown")}
  152.    this.OnMiddleDblClick  = {;ShowEvent(form.className+"."+this.name,"OnMiddleDblClick")}
  153.    this.OnLeftMouseUp     = {;ShowEvent(form.className+"."+this.name,"OnLeftMouseUp")}
  154.    this.OnLeftMouseDown   = {;ShowEvent(form.className+"."+this.name,"OnLeftMouseDown")}
  155.    this.OnLeftDblClick    = {;ShowEvent(form.className+"."+this.name,"OnLeftDblClick")}
  156.    this.OnLostFocus       = {;ShowEvent(form.className+"."+this.name,"OnLostFocus")}
  157.    this.OnGotFocus        = {;ShowEvent(form.className+"."+this.name,"OnGotFocus")}
  158.    this.OnOpen            = {;ShowEvent(form.className+"."+this.name,"OnOpen")}
  159.    this.When              = {||ShowEvent(form.className+"."+this.name,"When")}
  160.    this.OnHelp            = {;ShowEvent(form.className+"."+this.name,"OnHelp")}
  161.    this.OnClick           = {;ShowEvent(form.className+"."+this.name,"OnClick")}
  162.  
  163. endclass   && EventButton
  164.  
  165.  
  166. *******************************************************************************
  167. *******************************************************************************
  168. class ButtonForm(name, titleText) of EventForm(name, titleText)
  169.  
  170. * Create a form with a button that will move to new coordinates inside
  171. * the form when the left mouse button is clicked (and some other funcky stuff).
  172. *******************************************************************************
  173.  
  174. this.statusmessage = "Click - move button, Ctrl+Click - copy button,;
  175.    Shift+Click - allign buttons."
  176.  
  177.  
  178.    *******************************************************************************
  179.    procedure OnSize (nType,width,height)
  180.    *******************************************************************************
  181.    local i, r, c, newBHeight, newBWidth, button, hRatio, wRatio
  182.  
  183.    if height <> form.curHeight .or. width <> form.curWidth
  184.       form.OnLeftMouseDown(MK_SHIFT,0,0)       && Align all buttons
  185.       hRatio = height/form.curHeight
  186.       wRatio = width/form.curWidth
  187.       newBHeight = form.buttonHeight * hRatio  && Proportional
  188.       newBWidth  = form.buttonWidth  * wRatio  && button height
  189.       r = 0                                    && and width
  190.       c = 0
  191.       for i = 1 to form.buttonCnt
  192.          button = form[i]
  193.          button.top     = r * newBHeight
  194.          button.left    = c * newBWidth
  195.          button.height  = newBHeight
  196.          button.width   = newBWidth
  197.          button.text    = TRIMSTR(button.top) + "," + TRIMSTR(button.left)
  198.          button.fontSize = button.fontSize * (hRatio + wRatio)/2
  199.          button.visible = .T.
  200.          r = r + 1
  201.          if (r = form.totalRows)
  202.             r = 0
  203.             c = c + 1
  204.          endif
  205.       next i
  206.       form.curHeight    = height               && Reset base height, width
  207.       form.curWidth     = width                && for buttons and current
  208.       form.buttonHeight = newBHeight           && height, width for form
  209.       form.buttonWidth  = newBWidth
  210.       ShowEvent(this.name,"OnSize")
  211.    endif
  212.  
  213.  
  214.    *******************************************************************************
  215.    procedure OnOpen
  216.  
  217.  
  218.    * Give button its location and font
  219.    ********************************************************************************
  220.  
  221.    this.OnLeftMouseDown(0,this[1].left,this[1].top)
  222.  
  223.  
  224.    *******************************************************************************
  225.    procedure OnLeftMouseDown(flags, col, row)
  226.  
  227.    * OnLeftMouseDown event handler for buttonForm
  228.    *
  229.    * Click                -- moves last button to current location
  230.    * Shift + click        -- aligns buttons in form
  231.    * Ctrl + click         -- adds a new button at current location
  232.    *
  233.    *******************************************************************************
  234.    private f, i, r, c, button, maxCol, maxRow, bHeight, bWidth, bCnt
  235.  
  236.    bHeight   = form[1].height
  237.    bWidth    = form[1].width
  238.    if bitand(flags,MK_SHIFT) = MK_SHIFT  && SHIFT key was pressed
  239.       r = 0
  240.       c = 0
  241.       for i = 1 to form.buttonCnt
  242.          button      = form[i]
  243.          button.top  = r * bHeight
  244.          button.left = c * bWidth
  245.          button.text = TRIMSTR(button.top) + "," + TRIMSTR(button.left)
  246.          r = r + 1
  247.          if (r = form.totalRows)
  248.             r = 0
  249.             c = c + 1
  250.          endif
  251.       next i
  252.    else
  253.       if bitand(flags,MK_CONTROL) = MK_CONTROL  && Create another button
  254.          *** create another button
  255.          this.buttonCnt = form.buttonCnt + 1    && Increase button count
  256.          form[form.buttonCnt] = new EventButton(form)
  257.          form[form.buttonCnt].visible = .F.
  258.       endif
  259.       button = form[form.buttonCnt]   && temporary reference, for easier reading
  260.       button.text = TRIMSTR(row) + "," + TRIMSTR(col)
  261.       button.left = col
  262.       button.top  = row
  263.       button.visible = .T.                 && Make button visible only
  264.                                            && after all changes to it have been
  265.                                            && made.
  266.    endif
  267.    button.setFocus()    && So the status message would appear
  268.  
  269. endclass   && ButtonForm
  270.  
  271.  
  272. *******************************************************************************
  273. *******************************************************************************
  274. class SizeForm(name, titleText) of EventForm(name, titleText)
  275.  
  276. * Create  a form that will get resized as you click
  277. * the left mouse button and move.
  278. *******************************************************************************
  279.  
  280.    this.top = 12.86
  281.    this.width = 51.43
  282.    this.height = 8.08
  283.    this.OnSize = CLASS::SizeButton  && Resize the button when form is resized
  284.    this.OnSize()
  285.    this[1].text = "Button"
  286.  
  287.    *******************************************************************************
  288.    procedure OnLeftMouseDown(flags, x, y)
  289.  
  290.    *  Event handler for sizeForm.
  291.    *  Makes the size of the form change depending on the location of the mouse
  292.    *  while the mouse is pressed.
  293.    *******************************************************************************
  294.  
  295.    this.OnSize = .F.
  296.    SetCapture(this.hwnd)           && Current window receives all mouse messages
  297.                                    && Resize form and button as mouse moves
  298.    this.OnMouseMove = CLASS::SizeTheForm
  299.  
  300.  
  301.    *******************************************************************************
  302.    procedure OnLeftMouseUp
  303.  
  304.    *  Event handler for sizeForm.
  305.    *  Stop resizing the form while mouse is pressed and moving.  Now assign the
  306.    *  Button resizing to the OnSize event.
  307.    *******************************************************************************
  308.  
  309.    SetCapture(0)
  310.    this.OnMouseMove = .F.
  311.    this.OnSize = CLASS::SizeButton      && Resize button when form changes size
  312.  
  313.  
  314.    *******************************************************************************
  315.    procedure SizeTheForm(flags, col, row)
  316.  
  317.    * Changes the size of the form to reflect the current mouse position.
  318.    *******************************************************************************
  319.  
  320.    this.height = iif(row < 1, 1, row)   && Make sure numbers not negative
  321.    this.width = iif(col < 1, 1, col)
  322.    this.SizeButton()
  323.  
  324.  
  325.    *******************************************************************************
  326.    function SizeButton
  327.  
  328.    * Changes button to always be in the center of the form, and the same size
  329.    * relative to the size of the form.
  330.    *******************************************************************************
  331.    local button
  332.  
  333.    button = this[1]
  334.    button.fontName = "Serif"
  335.    button.height = this.height/3
  336.    button.width = this.width/3
  337.    button.top = this.height/3.03
  338.    button.left = this.width/2.55
  339.  
  340.    return .T.
  341.  
  342. endclass   && SizeForm
  343.  
  344.  
  345. *******************************************************************************
  346. function ShowEvent(name, eventName)
  347.  
  348. * Display the object name in proper case, and the event that happened.
  349. *******************************************************************************
  350.  
  351. ?proper(name), " -- ", eventName
  352. return .T.
  353.  
  354.  
  355.